package com.tanhua.manage.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.tanhua.manage.service.AnalysisService;
import com.tanhua.manage.service.UserService;
import com.tanhua.manage.util.ComputeUtil;
import com.tanhua.manage.vo.AnalysisDistributionVo;
import com.tanhua.manage.vo.AnalysisSummaryVo;
import com.tanhua.manage.vo.AnalysisUsersVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 首页界面
 * @author mxk
 * @date 2021/3/29 16:20
 */
@RestController
@RequestMapping("/management")
@Slf4j
public class AnalysisController {

    @Autowired
    private UserService userService;
    @Autowired
    private AnalysisService analysisService;
    /**
     *  1.概要统计接口
     */
    @GetMapping("/dashboard/summary")
    public AnalysisSummaryVo getSummary(){
        AnalysisSummaryVo analysisSummaryVo = new AnalysisSummaryVo();
//        DateTime dateTime= DateUtil.date();
        DateTime dateTime = DateUtil.parseDate("2020-09-08");
        //累计用户数
        analysisSummaryVo.setCumulativeUsers(Long.valueOf(userService.count()));

        //过去30天活跃用户
        analysisSummaryVo.setActivePassMonth(analysisService.queryActiveUserCountMonth(dateTime));

        //过去7天活跃用户数量
        analysisSummaryVo.setActivePassWeek(analysisService.queryActiveUserCountWeek(dateTime));

        //今日活跃用户
        analysisSummaryVo.setActiveUsersToday(analysisService.queryActiveUserCountDay(dateTime));

        //昨日活跃用户数量
        analysisSummaryVo.setActiveUsersYesterday(analysisService.queryActiveUserCountYesterday(dateTime));

        //今日新增用户数
        analysisSummaryVo.setNewUsersToday(analysisService.queryRegisterUserCount(dateTime,0));

        //今日新增用户涨跌率 单位百分数  正数为涨 负数为跌
        analysisSummaryVo.setNewUsersTodayRate(ComputeUtil.computeRate(
                analysisSummaryVo.getNewUsersToday(),analysisService.queryRegisterUserCount(dateTime,-1)));

        //今日登录次数
        analysisSummaryVo.setLoginTimesToday(analysisService.queryLoginUserCount(dateTime,0));

        //今日登录次数涨跌率  单位百分数  正数为涨 负数为跌
        analysisSummaryVo.setNewUsersTodayRate(ComputeUtil.computeRate(
                analysisSummaryVo.getLoginTimesToday(),analysisService.queryLoginUserCount(dateTime,-1)
        ));

        //今日活跃用户涨跌率  单位百分数  正数为涨 负数为跌
        analysisSummaryVo.setActiveUsersTodayRate(ComputeUtil.computeRate(
                analysisSummaryVo.getActiveUsersToday(),analysisSummaryVo.getActiveUsersYesterday()
        ));

        //过去7天平均日使用时长  没有数据 随机生成
        analysisSummaryVo.setUseTimePassWeek(RandomUtil.randomLong(600));

        //昨日活跃用户涨跌率  单位百分数  正数为涨 负数为跌
        analysisSummaryVo.setActiveUsersYesterdayRate(ComputeUtil.computeRate(
                analysisSummaryVo.getActiveUsersYesterday(),analysisService.queryActiveUserCountYesterday(DateUtil.offsetDay(dateTime, -1))
        ));
        return analysisSummaryVo;
    }

    /**
     * 2.新增  活跃用户  次日留存率
     */
    @GetMapping("/dashboard/users")
    public AnalysisUsersVo getUsers(@RequestParam(name = "sd")Long sd,@RequestParam(name = "ed")Long ed,
                                    @RequestParam(name = "type")Integer type){
        return analysisService.queryAnalysisUsersVo(sd,ed,type);
    }


    /**
     * 3.注册用户分布，行业top、年龄、性别、地区
     */
    @GetMapping("/dashboard/distribution")
    public AnalysisDistributionVo queryUserDistribution(@RequestParam(name = "sd")Long sd,@RequestParam(name = "ed")Long ed){
        sd=1598580158301L;
        ed=1601172158301L;
        AnalysisDistributionVo analysisDistributionVo = analysisService.queryUserDistribution(sd, ed);
        return analysisDistributionVo;
    }
}